﻿Imports System.Net
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Net.NetworkInformation

Public Class IPTools




    Private fThread As Thread
    Private fThread2 As Thread

    Public Delegate Sub AddRowDelegate(ByVal column As Integer)
    Delegate Sub CheckOnlineDelegate(ByVal rowindex As Integer)
    Delegate Sub SetOnlineDelegate(ByVal rowindex As Integer)

    Private Sub IPTools_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Sandbox Start
        '
        For Each IPAddress In Dns.GetHostAddresses(Dns.GetHostName)
            RTxBxAdaptIP.AppendText("NET: " & (IPAddress.ToString) & vbCrLf)
        Next IPAddress
        '
        Dim myNetworkAdapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
        Dim myAdapterProps As IPInterfaceProperties = Nothing
        Dim myGateways As GatewayIPAddressInformationCollection = Nothing

        For Each adapter As NetworkInterface In myNetworkAdapters
            myAdapterProps = adapter.GetIPProperties

            myGateways = myAdapterProps.GatewayAddresses

            For Each Gateway As GatewayIPAddressInformation In myGateways
                RTxBxAdaptIP.AppendText(("Gateway IP: " & Gateway.Address.ToString) & vbCrLf)
            Next
        Next
        '
        '
        Dim output As New System.Text.StringBuilder()

        For Each nic As Net.NetworkInformation.NetworkInterface In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
            If nic.OperationalStatus = Net.NetworkInformation.OperationalStatus.Up Then
                Dim unicastAddresses = From a As Net.NetworkInformation.UnicastIPAddressInformation In nic.GetIPProperties().UnicastAddresses() Where a.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork

                If unicastAddresses IsNot Nothing AndAlso unicastAddresses.Count > 0 Then
                    output.AppendLine(String.Format("Name: {0}", nic.Name))
                    output.AppendLine(String.Format("Status: {0}", nic.OperationalStatus))
                    output.AppendLine(String.Format("Description: {0}", nic.Description))
                    'output.AppendLine(String.Format("TEST: {0}", nic.Speed))
                    'output.AppendLine(String.Format("TEST: {0}", nic.GetPhysicalAddress))
                    output.AppendLine(String.Format("Interface Type: {0}", nic.NetworkInterfaceType.ToString()))
                    For Each address As Net.NetworkInformation.UnicastIPAddressInformation In unicastAddresses
                        output.AppendLine(String.Format("IP Address: {0}", address.Address.ToString()))
                    Next
                    output.AppendLine("")
                End If
            End If
        Next

        TextBox1.Text = output.ToString()

        'Sandbox End




        TxtB_PingRnge.Text = My.Settings.DefaultScanRange
        fgrid.Columns.Add("IP", "IP")
        fgrid.Columns.Add("Ping", "Ping")
        fgrid.Columns(0).Width = 100
        fgrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        For i As Integer = 0 To 254
            Dim myRowIndex = fgrid.Rows.Add()
            'fgrid.Rows(myRowIndex).Cells(0).Value = "192.168.1." & (1 + myRowIndex).ToString
            fgrid.Rows(myRowIndex).Cells(0).Value = My.Settings.DefaultScanRange & (1 + myRowIndex).ToString
        Next

    End Sub

    Private Sub ThreadProc()
        Parallel.For(0, 255, Sub(b)
                                 Do While Not done.Contains(b)
                                     fgrid.Invoke(New AddRowDelegate(AddressOf AddRow), New Object() {b})
                                     Thread.Sleep(300)
                                 Loop
                                 fgrid.Invoke(New SetOnlineDelegate(AddressOf SetOnline), New Object() {b})
                             End Sub)
    End Sub

    Private Sub ThreadProc2()
        Parallel.For(0, 255, Sub(b)
                                 CheckOnline(b)
                             End Sub)
    End Sub


    Private Sub AddRow(ByVal rowindex As Integer)
        If fgrid.Rows(rowindex).Cells(1).Value Is Nothing OrElse fgrid.Rows(rowindex).Cells(1).Value.ToString.Contains(".....") OrElse Not fgrid.Rows(rowindex).Cells(1).Value.ToString.Contains("Pinging") Then
            fgrid.Rows(rowindex).Cells(1).Value = "Pinging 192.168.1." & (1 + rowindex).ToString & " "
        Else
            fgrid.Rows(rowindex).Cells(1).Value = fgrid.Rows(rowindex).Cells(1).Value.ToString & "."
        End If
        fgrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.White
    End Sub

    Private success As New List(Of Integer)
    Private done As New List(Of Integer)

    Private Sub CheckOnline(ByVal rowindex As Integer)
        Dim _ping As New Ping
        Try
            Dim _pingreply = _ping.Send(My.Settings.DefaultScanRange & (1 + rowindex).ToString, 2000)
            If _pingreply.Status = IPStatus.Success Then
                SyncLock success
                    success.Add(rowindex)
                End SyncLock
            End If
        Catch ex As Exception
        End Try
        SyncLock done
            done.Add(rowindex)
        End SyncLock
    End Sub

    Private Sub SetOnline(ByVal rowindex As Integer)
        If Not success.Contains(rowindex) Then
            fgrid.Rows(rowindex).Cells(1).Value = "Offline"
            fgrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Red
        Else
            fgrid.Rows(rowindex).Cells(1).Value = "Online =]"
            fgrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Green

        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        done = New List(Of Integer)
        success = New List(Of Integer)
        fThread = New Thread(New ThreadStart(AddressOf ThreadProc))
        fThread.IsBackground = True
        fThread.Start()
        fThread2 = New Thread(New ThreadStart(AddressOf ThreadProc2))
        fThread2.IsBackground = True
        fThread2.Start()

    End Sub


    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles fgrid.CellContentClick
        '  Dim value As Object = fgrid.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

        'If IsDBNull(value) Then
        'TxtBx_Target.Text = "" ' blank if dbnull values
        'Else
        'TxtBx_Target.Text = CType(value, String)
        'End If
    End Sub

    Private Sub BtnTest_Click(sender As Object, e As EventArgs) Handles BtnTest.Click
        Dim osInfo As System.OperatingSystem = System.Environment.OSVersion
        'Dim systemName As String
        'systemName = CompNameTxtBox.Text
        If osInfo.Version.Major <= 5 Then
            'XP or Lower
 
            Process.Start("mstsc")
        Else
            'Vista / 2008 Server
            System.Diagnostics.Process.Start("mstsc.exe", "/v " & TxtBx_Target.Text)
            'Process.Start("mstsc" & " /v:" & "systemName")
        End If
        'Dim Command As String
        'Command = "taskmgr"
        'Shell("cmd /k" & Command, 1, True)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim Command As String
        Command = ("C:\Windows\System32\ping.exe -t " & TxtBx_Target.Text)
        Shell("cmd /k" & Command, 1, True)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim Command As String
        Command = ("C:\Windows\System32\arp.exe -a " & TxtBx_Target.Text)
        Shell("cmd /k" & Command, 1, True)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        My.Settings.DefaultScanRange = TxtB_PingRnge.Text
        My.Settings.Save()
  
    End Sub

    Private Sub BtnSetupTargets_Click(sender As Object, e As EventArgs) Handles BtnSetupTargets.Click
        fgrid.Rows.Clear()
        fgrid.Columns.Clear()

        fgrid.Columns.Add("IP", "IP")
        fgrid.Columns.Add("Ping", "Ping")
        fgrid.Columns(0).Width = 100
        fgrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        For i As Integer = 0 To 254
            Dim myRowIndex = fgrid.Rows.Add()
            'fgrid.Rows(myRowIndex).Cells(0).Value = "192.168.1." & (1 + myRowIndex).ToString
            fgrid.Rows(myRowIndex).Cells(0).Value = TxtB_PingRnge.Text & (1 + myRowIndex).ToString
        Next
    End Sub
End Class